//
//  CMDBCommentCell.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/15.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBCommentCell: CustomBaseCell {
    
    var iconImageView : UIImageView = UIImageView.getInstance()
    
    var nameLabel : UILabel         = UILabel.getInstance()
    
    var timeLabel : UILabel         = UILabel.getInstance()
    
    var contentLabel : UILabel      = UILabel.getInstance()
    
    var contentSize : CGSize?
    
    override func setupCell() {
        super.setupCell()
        
        backgroundColor = UIColor.whiteColor()
    }
    
    override func cellBuildSubViews() {
        super.cellBuildSubViews()
        
        iconImageView.frame               = CGRect(x: KleftMargin, y: KleftMargin, width: 40, height: 40)
        
        iconImageView.layer.cornerRadius  = 5
        
        iconImageView.layer.masksToBounds = true
        
        contentView.addSubview(iconImageView)
        
        nameLabel.frame                   = CGRect(x: CGRectGetMaxX(self.iconImageView.frame) + KleftMargin, y: KleftMargin, width: 100, height: 16)
        nameLabel.font                    = CMDB_16_Bold_FONT
        
        contentView.addSubview(nameLabel)
        
        timeLabel.font                    = CMDB_12_FONT
        timeLabel.textColor               = word38Color
        timeLabel.sizeToFit()
        
        contentView.addSubview(timeLabel)
        
        contentLabel.numberOfLines        = 0
        
        contentLabel.font                 = CMDB_16_FONT
        
        contentLabel.textColor            = word87Color
        
        contentView.addSubview(contentLabel)
        
        let bottomLine         = UIView.getInstance()
        
        bottomLine.backgroundColor = UIColor.Hex6(0x000000, alpha: 0.12)
        
        contentView.addSubview(bottomLine)
        
        bottomLine.snp_makeConstraints { (make) in
            make.left.equalTo(0);
            make.right.equalTo(0);
            make.bottom.equalTo(0);
            make.height.equalTo(0.5)
        }
    }
    
    override func loadContent() {
        let commentData     = cellData as! CMDBCommentBean
        
        iconImageView.setHeader(commentData.icon)
        
        nameLabel.text      = commentData.name
        
        timeLabel.text      = commentData.commentTimeFormat
        
        timeLabel.sizeToFit()
        
        timeLabel.frame = CGRect(x: CMDB_Screen_Width - KleftMargin - timeLabel.width, y: 20, width: timeLabel.width, height: timeLabel.height)
        
        contentLabel.text   = commentData.content
        
        contentSize         = commentData.content.suitToSize(CGSize(width: CMDB_Screen_Width - 2 * KleftMargin,height: 999), fontSize: 16, lineSpacing: 2)
        
        contentLabel.frame  = CGRect(x: KleftMargin, y: CGRectGetMaxY(self.iconImageView.frame) + 0.5 * KleftMargin, width: CMDB_Screen_Width - 2 * KleftMargin, height: contentSize!.height)
        
        
        if commentData.cellHeight == 0 {
            
            layoutIfNeeded()
            
            commentData.cellHeight = CGRectGetMaxY(contentLabel.frame) + 5 + 10
        }


    }
    
    
}
